;; Tests of the Non-robust matrix functions
;; Copyright (C) 2021  Rocks Mazama
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

(ns mazama.linalg.matrix-test
  (:require
   [clojure.test :refer [deftest is testing]]
   [mazama.linalg.matrix :as matrix]))

(deftest matrix-column
  (testing "Extract a column"
    (let [m {:ncols 3 :data [1 2 3 4 5 6 7 8 9]}]
      (is (= (matrix/column 0 0 m) [1 4 7]))
      (is (= (matrix/column 1 1 m) [5 8]))
      (is (= (matrix/column 1 2 m) [6 9]))
      (is (= (matrix/column 2 1 m) [8]))
      (is (= (matrix/column 2 2 m) [9])))))

(deftest matrix-columns
  (testing "Convert a matrix to sequence of columns"
    (let [m {:ncols 3 :data [1 2 3 4 5 6 7 8 9]}]
      (is (= (matrix/columns m)
             [[1 4 7]
              [2 5 8]
              [3 6 9]])))))

(deftest matrix-columns->matrix
  (testing "Convert columns to a matrix"
    (let [columns [[1 5]
                   [2 6]
                   [3 7]
                   [4 8]]]
      (is (= (matrix/columns->matrix columns)
             {:ncols 4 :data [1 2 3 4 5 6 7 8]})))))

(deftest matrix-row
  (testing "Extract a row"
    (let [m {:ncols 4 :data [1 2 3 4 5 6 7 8]}]
      (is (= (matrix/row 0 0 m) [1 2 3 4]))
      (is (= (matrix/row 0 1 m) [2 3 4]))
      (is (= (matrix/row 1 0 m) [5 6 7 8]))
      (is (= (matrix/row 1 1 m) [6 7 8]))
      (is (= (matrix/row 1 3 m) [8])))))

(deftest matrix-rows
  (testing "Convert a matrix to sequence of rows"
    (let [m {:ncols 3 :data [1 2 3 4 5 6 7 8 9]}]
      (is (= (matrix/rows m)
             [[1 2 3]
              [4 5 6]
              [7 8 9]])))))

(deftest matrix-rows->matrix
  (testing "Convert rows to a matrix"
    (let [rows [[1 2 3 4]
                [5 6 7 8]]]
      (is (= (matrix/rows->matrix rows)
             {:ncols 4 :data [1 2 3 4 5 6 7 8]})))))
